﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ConnectCsharpToMysql.objects
{
    class Category
    {
        public String idCategory;
        public String nameCategory;
        public String desCategory;
        public String avatar;
        public String createDate;
        public String status;

        public Category() { }

        public Boolean select() {
            String strSelectQuery = "SELECT * FROM tbcategory WHERE idCategory = " + idCategory;
            DataTable dataTable = new DBConnect().Select(strSelectQuery);
            if (dataTable.Rows.Count > 0)
            {
                nameCategory = dataTable.Rows[0]["nameCategory"].ToString();
                desCategory = dataTable.Rows[0]["desCategory"].ToString();
                avatar = dataTable.Rows[0]["avatar"].ToString();
                createDate = dataTable.Rows[0]["createDate"].ToString();
                status = dataTable.Rows[0]["status"].ToString();
                return true;
            }
            else {
                return false;
            }
        }

        public static List<Category> selectAll() {
            List<Category> listCategory = new List<Category>();
            String strSelectQuery = "SELECT * FROM tbcategory";
            DataTable dataTable = new DBConnect().Select(strSelectQuery);
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                Category category = new Category();
                category.idCategory = dataTable.Rows[0]["idCategory"].ToString();
                category.nameCategory = dataTable.Rows[0]["nameCategory"].ToString();
                category.desCategory = dataTable.Rows[0]["desCategory"].ToString();
                category.avatar = dataTable.Rows[0]["avatar"].ToString();
                category.createDate = dataTable.Rows[0]["createDate"].ToString();
                category.status = dataTable.Rows[0]["status"].ToString();
                listCategory.Add(category);
            }
            return listCategory;
        }

        public Boolean insert()
        {
            String strInserQuery = "INSERT INTO tbcategory(nameCategory,desCategory,avatar,createDate,status) " +
                                    "VALUES(N'" + nameCategory + "',N'" + desCategory + "','" + avatar + "','" + createDate + "',N'" + status + "')";
            return new DBConnect().Insert(strInserQuery);
        }

        public Boolean update()
        {
            String strUpdateQuery = "UPDATE tbcategory " +
                        "SET nameCategory=N'" + nameCategory + "', desCategory=N'" + desCategory + "',avatar='" + avatar + "',status=" + status + " " +
                        "WHERE idCategory=" + idCategory;
            return new DBConnect().Update(strUpdateQuery);
        }

        public Boolean delete()
        {
            String strDeleteQuery = "DELETE FROM tbcategory WHERE idCategory = " + idCategory;
            return new DBConnect().Delete(strDeleteQuery);
        }

    }
}
